@echo off
chcp 65001 >nul
echo ========================================
echo    RocketMQ项目一键部署脚本
echo ========================================
echo.

REM 设置变量
set SERVER_IP=192.168.211.139
set SERVER_USER=root
set SSH_KEY=C:\Users\Teriberka\.ssh\my_linux_key
set PROJECT_NAME=rocketmq-rate-limit-demo

echo 服务器信息:
echo - IP地址: %SERVER_IP%
echo - 用户名: %SERVER_USER%
echo - SSH密钥: %SSH_KEY%
echo.

REM 检查Maven是否安装
echo 检查环境...
mvn --version >nul 2>&1
if %errorlevel% neq 0 (
    echo ✗ Maven未安装或不在PATH中
    pause
    exit /b 1
)
echo ✓ Maven已安装
echo.

REM 清理旧的构建文件
echo 1. 清理旧的构建文件...
if exist target\*.jar del /q target\*.jar
echo ✓ 清理完成
echo.

REM Maven构建项目
echo 2. Maven构建项目...
mvn clean package -DskipTests
if %errorlevel% neq 0 (
    echo ✗ Maven构建失败
    pause
    exit /b 1
)
echo ✓ Maven构建成功
echo.

REM 检查jar包是否存在
if not exist target\%PROJECT_NAME-*.jar (
    echo ✗ 未找到构建的jar包
    pause
    exit /b 1
)

REM 获取jar包名称
for %%f in (target\%PROJECT_NAME-*.jar) do set JAR_FILE=%%f
echo 找到jar包: %JAR_FILE%
echo.

REM 创建临时部署目录
echo 3. 准备部署文件...
if not exist deploy mkdir deploy
copy %JAR_FILE% deploy\app.jar >nul
copy Dockerfile deploy\ >nul
copy broker.conf deploy\ >nul
echo ✓ 部署文件准备完成
echo.

REM 上传文件到服务器
echo 4. 上传文件到服务器...
scp -i "%SSH_KEY%" -r deploy\* %SERVER_USER%@%SERVER_IP%:/home/rocketmq/
if %errorlevel% neq 0 (
    echo ✗ 文件上传失败
    pause
    exit /b 1
)
echo ✓ 文件上传成功
echo.

REM 在服务器上构建和部署
echo 5. 在服务器上构建和部署...
ssh -i "%SSH_KEY%" %SERVER_USER%@%SERVER_IP% << 'EOF'
cd /home/rocketmq

echo "停止现有容器..."
docker stop rocketmq-app 2>/dev/null || true
docker rm rocketmq-app 2>/dev/null || true

echo "删除旧镜像..."
docker rmi rocketmq-app:latest 2>/dev/null || true

echo "构建新镜像..."
docker build -t rocketmq-app:latest .

if [ $? -eq 0 ]; then
    echo "镜像构建成功，启动应用容器..."
    docker run -d \
      --name rocketmq-app \
      --network rmq \
      -p 8080:8080 \
      -e SPRING_PROFILES_ACTIVE=prod \
      -e ROCKETMQ_NAMESRV_ADDR=192.168.211.139:9876 \
      -v /home/rocketmq/logs/app:/app/logs \
      --restart unless-stopped \
      rocketmq-app:latest

    echo "等待应用启动..."
    sleep 15

    echo "检查容器状态..."
    docker ps | grep rocketmq-app

    echo "检查应用日志..."
    docker logs rocketmq-app --tail 10

    echo "测试应用健康状态..."
    if curl -f http://localhost:8080/api/rocketmq/health >/dev/null 2>&1; then
        echo "✓ 应用健康检查通过！"
    else
        echo "⚠ 应用还未完全启动，请稍后检查"
    fi

    echo "清理临时文件..."
    rm -f app.jar Dockerfile broker.conf
else
    echo "✗ 镜像构建失败！"
    exit 1
fi
EOF

if %errorlevel% neq 0 (
    echo ✗ 服务器部署失败
    pause
    exit /b 1
)
echo ✓ 服务器部署成功
echo.

REM 清理本地临时文件
echo 6. 清理本地临时文件...
rmdir /s /q deploy
echo ✓ 清理完成
echo.

echo ========================================
echo           部署完成！
echo ========================================
echo.
echo 应用访问地址: http://%SERVER_IP%:8080
echo 健康检查: http://%SERVER_IP%:8080/api/rocketmq/health
echo.
echo 常用命令:
echo - 查看容器状态: docker ps
echo - 查看应用日志: docker logs rocketmq-app
echo - 重启应用: docker restart rocketmq-app
echo - 停止应用: docker stop rocketmq-app
echo.

echo 按任意键退出...
pause >nul
